package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.UpdateCdbPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IUpdateCdb;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;

/**
 * 作者：Administrator on 2017/7/5 15:16
 * 邮箱：zhanghuaiha@gmail.com
 * 变更设备
 */

public class UpdateCdbActivity extends BaseActivity<IUpdateCdb,UpdateCdbPresenter> implements IUpdateCdb {
    private ImageView back;
    private TextView update_id,update_name,update_save,update_number;
    private EditText update_remarks,update_user;
    private static final int REQUEST_SEARCH_CODE = 1;
    private String alias;
    private String udid;
    private String id;

    @Override
    public void initView() {
        back = (ImageView) findViewById(R.id.update_back);
        update_id = (TextView) findViewById(R.id.update_id);
        update_name = (TextView) findViewById(R.id.update_name);
        update_save = (TextView) findViewById(R.id.update_save);
        update_number = (TextView) findViewById(R.id.update_number);
        update_remarks = (EditText) findViewById(R.id.update_remarks);
        update_user = (EditText) findViewById(R.id.update_user);
    }

    @Override
    public void initData() {
        alias = getIntent().getStringExtra("alias");
        udid = ObtainUdis.getUDID(NetInActivity.class);
        update_id.setText(alias);
        mPresenter.loadContacts(udid,alias);
    }

    @Override
    public void initListener() {
        update_save.setOnClickListener(v -> {
            String remarks = update_remarks.getText().toString().trim();
            String user = update_user.getText().toString().trim();
            if (TextUtils.isEmpty(remarks)) {
                remarks = "";
            }
            if (TextUtils.isEmpty(user)) {
                user = "";
            }

            mPresenter.loadData(alias,udid,id,user,remarks);

        });
        update_name.setOnClickListener(v -> {
            Intent intent = new Intent(UpdateCdbActivity.this,QueryActivity.class);
            startActivityForResult(intent,REQUEST_SEARCH_CODE);
        });
        back.setOnClickListener(v -> finish());
        update_remarks.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //  Log.e("aaa",s.length()+"");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // Log.e("bbbbb",s.length()+"");
            }

            @Override
            public void afterTextChanged(Editable s) {
                // Log.e("cccc",s.length()+"");
                update_number.setText(s.length()+"/100");

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == REQUEST_SEARCH_CODE) {
            if (data != null) {
                id = data.getStringExtra("id");
                String name = data.getStringExtra("name");
                update_name.setText(name);
            }
        }
    }


    @Override
    protected UpdateCdbPresenter createPresenter() {
        return new UpdateCdbPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_update_cdb;
    }

    @Override
    public EditText getET() {
        return update_user;
    }

    @Override
    public EditText getRemark() {
        return update_remarks;
    }

    @Override
    public TextView getTV() {
        return update_name;
    }
}
